<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书籍" prop="bookId">
        <el-select v-model="queryParams.bookId" placeholder="请选择书籍" clearable>
          <el-option
            v-for="item in bookList"
            :key="item.id"
            :label="item.bookName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="章节数" prop="chapterNumber">
        <el-input
          v-model="queryParams.chapterNumber"
          placeholder="请输入章节数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="章节标题" prop="chapterTitle">
        <el-input
          v-model="queryParams.chapterTitle"
          placeholder="请输入章节标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="章节字数" prop="chapterFontNumber">
        <el-input
          v-model="queryParams.chapterFontNumber"
          placeholder="请输入章节字数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否付费" prop="chapterIsPay">
        <el-select v-model="queryParams.chapterIsPay" placeholder="请选择是否付费" clearable>
          <el-option
            v-for="dict in dict.type.sys_novel_money"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新者" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入更新者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['novel:chapter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['novel:chapter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['novel:chapter:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['novel:chapter:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chapterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="书籍" align="center" prop="bookName" />
      <el-table-column label="章节数" align="center" prop="chapterNumber" />
      <el-table-column label="章节标题" align="center" prop="chapterTitle" />
      <el-table-column label="章节字数" align="center" prop="chapterFontNumber" />
      <el-table-column label="阅读数量" align="center" prop="chapterReadNumber" />
      <!--el-table-column label="章节内容" align="center" prop="chapterContent" /-->
      <el-table-column label="是否付费" align="center" prop="chapterIsPay" />
      <el-table-column label="付费金额" align="center" prop="chapterPayMoney" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['novel:chapter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['novel:chapter:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改章节列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书籍" prop="bookId">
          <el-select v-model="form.bookId" placeholder="请选择书籍">
            <el-option
              v-for="item in bookList"
              :key="item.id"
              :label="item.bookName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="章节数" prop="chapterNumber">
          <el-input v-model="form.chapterNumber" placeholder="请输入章节数" />
        </el-form-item>
        <el-form-item label="章节标题" prop="chapterTitle">
          <el-input v-model="form.chapterTitle" placeholder="请输入章节标题" />
        </el-form-item>
        <el-form-item label="章节字数" prop="chapterFontNumber">
          <el-input v-model="form.chapterFontNumber" placeholder="请输入章节字数" />
        </el-form-item>
        <el-form-item label="阅读数量" prop="chapterReadNumber">
          <el-input v-model="form.chapterReadNumber" placeholder="请输入阅读数量" />
        </el-form-item>
        <el-form-item label="章节内容">
          <editor v-model="form.chapterContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="是否付费" prop="chapterIsPay">
          <el-select v-model="form.chapterIsPay" placeholder="请选择是否付费" clearable>
            <el-option
              v-for="dict in dict.type.sys_novel_money"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="付费金额" prop="chapterPayMoney" v-show="form.chapterIsPay == '2'">
          <el-input v-model="form.chapterPayMoney" placeholder="请输入付费金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChapter, getChapter, delChapter, addChapter, updateChapter } from "@/api/novel/chapter";
import { listBook } from "@/api/novel/book";

export default {
  name: "Chapter",
  dicts: ["sys_novel_money"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 章节列表表格数据
      chapterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookId: null,
        chapterNumber: null,
        chapterTitle: null,
        chapterFontNumber: null,
        chapterIsPay: null,
        createBy: null,
        updateBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bookId: [
          { required: true, message: "书籍ID不能为空", trigger: "blur" }
        ],
        chapterNumber: [
          { required: true, message: "章节数不能为空", trigger: "blur" }
        ],
        chapterTitle: [
          { required: true, message: "章节标题不能为空", trigger: "blur" }
        ],
        chapterFontNumber: [
          { required: true, message: "章节字数不能为空", trigger: "blur" }
        ],
        chapterReadNumber: [
          { required: true, message: "阅读数量不能为空", trigger: "blur" }
        ],
        chapterContent: [
          { required: true, message: "章节内容不能为空", trigger: "blur" }
        ],
        chapterIsPay: [
          { required: true, message: "是否付费不能为空", trigger: "blur" }
        ],
      },
      //书籍列表
      bookList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询章节列表列表 */
    getList() {
      this.loading = true;
      listChapter(this.queryParams).then(response => {
        this.chapterList = response.rows;
        this.total = response.total;
        this.loading = false;
        //查询书籍列表
        this.getBookList();
      });
    },
    /** 查询书籍列表 */
    getBookList(){
      let que = {pageSize: 99999};
      listBook(que).then(response => {
        this.bookList = response.rows;
        console.log(this.bookList)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bookId: null,
        chapterNumber: null,
        chapterTitle: null,
        chapterFontNumber: null,
        chapterReadNumber: null,
        chapterContent: null,
        chapterIsPay: null,
        chapterPayMoney: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加章节列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChapter(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改章节列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChapter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChapter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除章节列表编号为"' + ids + '"的数据项？').then(function() {
        return delChapter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('novel/chapter/export', {
        ...this.queryParams
      }, `chapter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
